package routers

import (
	"net/http"
	"time"

	"shqsoft.cn/pasnj/base"
	"shqsoft.cn/pasnj/entity"
	"shqsoft.cn/pasnj/popedom"

	//log "github.com/sirupsen/logrus"
	jwt "github.com/dgrijalva/jwt-go"
	"github.com/labstack/echo"
)

// MenuByParentRouter 路由：根据给定的ParentMenuID获取其下的子菜单
func MenuByParentRouter(c echo.Context) error {
	pMenuID := c.Param("ParentMenuID")
	if pMenuID == "" {
		return echo.NewHTTPError(http.StatusBadRequest, "参数ParentMenuID不能为空！")
	}
	var DaoName string
	DaoName = "Menu"
	dao, err := base.GetDao(DaoName)
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	listData, err := dao.Find2(" ISQY=1 AND ParentPermissionId=?", []interface{}{pMenuID}, nil, 0, 0)
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	return c.JSON(http.StatusOK, listData)
}

// SysInfo 系统信息
type SysInfo struct {
	Menus     []entity.Menu `json:"menus"`
	UserID    int           `json:"userID"`
	OrgID     int           `json:"orgID"`
	POrgID    int           `json:"pOrgID"`
	OdsDate   time.Time     `json:"odsDate"`
	OdsDatesy time.Time     `json:"odsDatesy"`
	CMDate    time.Time     `json:"cmDate"`
	UserName  string        `json:"userName"`
	UserNo    string        `json:"UserNo"`
	Sjy       string        `json:"sjy"`
	RDDate    time.Time     `json:"RDDate"`
	IsInfo    int           `json:"IsInfo"`
	Infoname  string        `json:"Infoname"`
	FTPDate   time.Time     `json:"FTPDate"`
	LSGHR     int           `json:"LSGHR"`
}

// MenuAllRouter 获取所有的权限
func MenuAllRouter(c echo.Context) error {
	user := c.Get("user").(*jwt.Token)
	claims := user.Claims.(*UserLoginClaims)
	var okMenus []entity.Menu
	var err error
	okMenus, err = getEWDMenus(claims)
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	var odsDate time.Time
	var odsDatesy time.Time
	var cmDate time.Time
	var userNAME string
	var userNO string
	var rdDate time.Time
	var ftpDate time.Time
	var lsghr int

	row := base.DB.Raw("SELECT SYDT FROM T_CONF_SYDT").Row()
	err = row.Scan(&odsDate)
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	row = base.DB.Raw("SELECT DATEADD(DAY,-1,DBO.GETmonthFirstDAY(SYDT)) SYDT FROM T_CONF_SYDT").Row()
	err = row.Scan(&odsDatesy)
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	row = base.DB.Raw("SELECT SYDT FROM T_CONF_SYDT_SC").Row()
	err = row.Scan(&cmDate)
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	row = base.DB.Raw("SELECT SYDT2 FROM T_CONF_SYDT_SC").Row()
	err = row.Scan(&ftpDate)
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	row = base.DB.Raw("SELECT SysUserID,SysUserName,ISLSGHR FROM T_SysUser WHERE ID=?", claims.UserID).Row()
	err = row.Scan(&userNO, &userNAME, &lsghr)
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}

	row = base.DB.Raw("SELECT dbo.GetFlowBackDate()").Row()
	err = row.Scan(&rdDate)
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}

	var (
		isInfo   int
		infoname string
	)
	rows, err := base.DB.Raw("SELECT ISQY,NAME FROM T_CONF_INFO").Rows()
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	defer rows.Close()
	if rows.Next() {
		rows.Scan(&isInfo, &infoname)
	}

	sysInfo := SysInfo{
		Menus:     okMenus,
		UserID:    claims.UserID,
		OrgID:     claims.OrganID,
		POrgID:    claims.ParentOrgID,
		OdsDate:   odsDate,
		OdsDatesy: odsDatesy,
		CMDate:    cmDate,
		UserName:  userNAME,
		UserNo:    userNO,
		Sjy:       claims.Sjy,
		RDDate:    rdDate,
		IsInfo:    isInfo,
		Infoname:  infoname,
		FTPDate:   ftpDate,
		LSGHR:     lsghr,
	}
	return c.JSON(http.StatusOK, sysInfo)
}
func getEWDMenus(claims *UserLoginClaims) ([]entity.Menu, error) {
	var menus []entity.Menu
	err := base.DB.Where(" ISQY=1 ").Order("ParentPermissionId,SortNo").Find(&menus).Error
	if err != nil {
		return nil, err
	}
	okMenus := make([]entity.Menu, 0, len(menus))
	for _, menu := range menus {
		isOk, err := popedom.ValidateUserPermission(claims.UserID, menu.ID)
		if err != nil {
			return nil, err
		}
		if isOk {
			pTypes, err := popedom.GetUserPermissionTypes(claims.UserID, menu.ID)
			if err != nil {
				return nil, err
			}
			menu.UserPopedomTypes = pTypes
			okMenus = append(okMenus, menu)
		}
	}
	return okMenus, nil
}
func getODSMenus(claims *UserLoginClaims) ([]entity.Menu, error) {
	var menus []entity.MenuOds
	err := base.DB.Where(" ISQY=1 ").Order("ParentPermissionId,SortNo").Find(&menus).Error
	if err != nil {
		return nil, err
	}
	okMenus := make([]entity.Menu, 0, len(menus))
	for _, menu := range menus {
		isOk, err := popedom.ValidateUserPermission(claims.UserID, menu.ID)
		if err != nil {
			return nil, err
		}
		if isOk {
			pTypes, err := popedom.GetUserPermissionTypes(claims.UserID, menu.ID)
			if err != nil {
				return nil, err
			}
			menu.UserPopedomTypes = pTypes
			menuItem := entity.Menu{
				ID:               menu.ID,
				No:               menu.No,
				ParentID:         menu.ParentID,
				Name:             menu.Name,
				PermissionType:   menu.PermissionType,
				IsUse:            menu.IsUse,
				SortNo:           menu.SortNo,
				MenusCaptions:    menu.MenusCaptions,
				ReactComponent:   menu.ReactComponent,
				ReactPorps:       menu.ReactPorps,
				ReactIcon:        menu.ReactIcon,
				UserPopedomTypes: menu.UserPopedomTypes,
				SJY:              menu.SJY,
			}
			okMenus = append(okMenus, menuItem)
		}
	}
	return okMenus, nil
}
